package org.BUPT.domain.service.impl;

import org.BUPT.domain.service.MessageService;
import org.BUPT.infrastructure.persistent.DAO.MessageDAO;
import org.BUPT.infrastructure.persistent.po.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageDAO messageDAO;

    public Message visitMessage(int messageId){
        Message message = messageDAO.findById(messageId).orElse(null);
        assert message!=null;
        message.setIsRead(1);
        return messageDAO.save(message);
    }

    public List<Message> getMessagesOfUser(int userId){
        return messageDAO.findMessagesByDestUserId(userId);
    }
    public Message getMessage(int messageId){return messageDAO.findById(messageId).orElse(null);}
    public void addMessage(Message message){
        messageDAO.save(message);
    }
}
